/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdTecnico.java
 *
 * Created on 12/02/2011, 01:08:18 AM
 */
package com.fsl.sicotec.UI;

import com.fsl.sicotec.Logica.LogTecnico;
import com.fsl.sicotec.comun.Entidades.Tecnico;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author User
 */
public class jdTecnico extends javax.swing.JDialog {

    /** Creates new form jdTecnico */
    public jdTecnico(jfListadoTecnicos parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    private jdTecnico(JFrame jFrame, boolean b) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    jdTecnico(jfListadoTecnicos parent, boolean modal, String strDNI) {
        super(parent, modal);
        initComponents();
        //lista = new ArrayList();
        jtxtDNI.requestFocusInWindow();
        this.setTitle("Actualizar Cliente");
        jtxtDNI.setText(strDNI);
        mostrarTecnico(strDNI);
    }

    private void mostrarTecnico(String DNI) {
        LogTecnico objLogTecnico = new LogTecnico();
        //Tecnico objTecnico = new Tecnico();
        ArrayList<Tecnico> arrTecnico = new ArrayList();
        arrTecnico = objLogTecnico.getTecnico(DNI);

        jtxtCodigo.setText(arrTecnico.get(0).getCod_tecnico());
        jtxtDNI.setText(arrTecnico.get(0).getDni());
        jtxtApePat.setText(arrTecnico.get(0).getApe_paterno());
        jtxtApeMat.setText(arrTecnico.get(0).getApe_materno());
        jtxtNombres.setText(arrTecnico.get(0).getNombres());
        jtxtDireccion.setText(arrTecnico.get(0).getDireccion());
        jtxtTelefono.setText(arrTecnico.get(0).getTelefono());
    }

    private void registrar() {
        LogTecnico objLogTecnico = new LogTecnico();
        Tecnico objTecnico = new Tecnico();
        objTecnico.setDni(this.jtxtDNI.getText());
        objTecnico.setApe_paterno(this.jtxtApePat.getText().trim().toUpperCase());
        objTecnico.setApe_materno(this.jtxtApeMat.getText().trim().toUpperCase());
        objTecnico.setNombres(this.jtxtNombres.getText().trim().toUpperCase());
        objTecnico.setDireccion(this.jtxtDireccion.getText().trim().toUpperCase());
        objTecnico.setTelefono(this.jtxtTelefono.getText().trim());
        try {
            objLogTecnico.registrarTecnico(objTecnico);
            setVisible(false);
        } catch (Exception e2) {
            JOptionPane.showMessageDialog(null, e2.getMessage());
        }
    }

    private void actualizar() {
        LogTecnico objLogTecnico = new LogTecnico();
        Tecnico objTecnico = new Tecnico();
        String DNI;
        DNI=this.jtxtDNI.getText();
        //objTecnico.setDni(this.jtxtDNI.getText());
        objTecnico.setApe_paterno(this.jtxtApePat.getText().trim().toUpperCase());
        objTecnico.setApe_materno(this.jtxtApeMat.getText().trim().toUpperCase());
        objTecnico.setNombres(this.jtxtNombres.getText().trim().toUpperCase());
        objTecnico.setDireccion(this.jtxtDireccion.getText().trim().toUpperCase());
        objTecnico.setTelefono(this.jtxtTelefono.getText().trim());
        try {
            objLogTecnico.actualizarTecnico(DNI, objTecnico);
            setVisible(false);
        } catch (Exception e2) {
            JOptionPane.showMessageDialog(null, e2.getMessage());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnAceptar = new javax.swing.JButton();
        jtxtApePat = new javax.swing.JTextField();
        jtxtDNI = new javax.swing.JTextField();
        jtxtCodigo = new javax.swing.JTextField();
        jbtnCancelar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtxtNombres = new javax.swing.JTextField();
        jtxtApeMat = new javax.swing.JTextField();
        jtxtTelefono = new javax.swing.JTextField();
        jtxtDireccion = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mantenimiento de Técnico");

        jbtnAceptar.setText("Aceptar");
        jbtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarActionPerformed(evt);
            }
        });

        jtxtApePat.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtApePatKeyPressed(evt);
            }
        });

        jtxtDNI.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtDNIKeyPressed(evt);
            }
        });

        jtxtCodigo.setEnabled(false);

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jLabel8.setText("Teléfono:");

        jLabel6.setText("Dirección:");

        jLabel5.setText("Nombres:");

        jLabel4.setText("APellido Materno:");

        jLabel3.setText("Apellido Paterno:");

        jLabel2.setText("DNI:");

        jLabel1.setText("Código:");

        jtxtNombres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtNombresKeyPressed(evt);
            }
        });

        jtxtApeMat.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtApeMatKeyPressed(evt);
            }
        });

        jtxtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtTelefonoKeyPressed(evt);
            }
        });

        jtxtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtDireccionKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtApePat, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtApeMat, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jtxtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(291, Short.MAX_VALUE)
                .addComponent(jbtnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jtxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jtxtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jtxtApePat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jtxtApeMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jtxtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jtxtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jtxtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnAceptar)
                    .addComponent(jbtnCancelar))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        if (this.jtxtDNI.getText().length() == 8) {
            if(this.jtxtCodigo.getText().isEmpty()){
                registrar();
            }else{
                actualizar();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese DNI");
            this.jtxtDNI.requestFocusInWindow();
        }
}//GEN-LAST:event_jbtnAceptarActionPerformed

    private void jtxtApePatKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtApePatKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            this.jtxtApeMat.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtApePatKeyPressed

    private void jtxtDNIKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtDNIKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            this.jtxtApePat.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtDNIKeyPressed

    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
       setVisible(false);
        
}//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jtxtNombresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtNombresKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            this.jtxtDireccion.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtNombresKeyPressed

    private void jtxtApeMatKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtApeMatKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            this.jtxtNombres.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtApeMatKeyPressed

    private void jtxtTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtTelefonoKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            // this.jtxtContacto.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtTelefonoKeyPressed

    private void jtxtDireccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtDireccionKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            this.jtxtTelefono.requestFocusInWindow();
        }
}//GEN-LAST:event_jtxtDireccionKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                jdTecnico dialog = new jdTecnico(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JTextField jtxtApeMat;
    private javax.swing.JTextField jtxtApePat;
    private javax.swing.JTextField jtxtCodigo;
    private javax.swing.JTextField jtxtDNI;
    private javax.swing.JTextField jtxtDireccion;
    private javax.swing.JTextField jtxtNombres;
    private javax.swing.JTextField jtxtTelefono;
    // End of variables declaration//GEN-END:variables
}
